package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.Impl.UserServiceImpl;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.utils.MD5Util;
import com.bjpowernode.crm.utils.PrintJson;
import com.bjpowernode.crm.utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class UserController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //一个模块功能一个控制器


        System.out.println("用户进入控制器！");
        String path = request.getServletPath();
        //有/的
        if ("/settings/user/login.do".equals(path)) {
            login(request, response);

        } else if ("/settings/user/".equals(path)) {
            System.out.println(path);
        }
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        response.setContentType("text/plain");
        response.setContentType("application/json");

        System.out.println("进入到验证登录的操作！");

        String loginAct = request.getParameter("loginAct");
        String loginPwd = request.getParameter("loginPwd");
        //将密码的明文转换为MD5的密文形式
        loginPwd = MD5Util.getMD5(loginPwd);
        //接收浏览器端的ip地址
        String ip = request.getRemoteAddr();
        System.out.println("-------ip:" + ip);

        //未来的业务层开发统一使用代理类的形态的接口对象
        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());
        try {
            //登录失败跳到catch块
            User user = us.login(loginAct, loginPwd, ip);
            HttpSession session = request.getSession();
            //session失效时间设为30分钟
            session.setMaxInactiveInterval(30 * 60);
            session.setAttribute("user", user);
            //程序执行到此处，说明业务层没有为controller跳出任何的异常
            //登录成功！
            /*
                返回值：
                {"success":ture}
             */
            PrintJson.printJsonFlag(response, true);
        } catch (Exception exception) {
            exception.printStackTrace();

            //一旦程序执行了catch块的信息，说明业务层为我们验证登录失败，为controller抛出了异常
            //表示登录失败!
            /*
                返回值：
                {"success":ture}
             */
            String msg = exception.getMessage();

            /*
                作为控制器要为AJAX提供多项信息

                两种方法：
                    （1）将多项信息打包为map,将map解析为json串
                    （2）创建一个Vo
                        private boolean success;
                        private String msg;


                    如果对于展现的信息还会大量的使用我们会创建vo类大量使用
                    如果对于展现的信息只有这个需求才能使用，我们使用map就可以了
             */
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("success", false);
            map.put("msg", msg);
            System.out.println("------------msg:" + msg);
            PrintJson.printJsonObj(response, map);
        }
    }
}
